package com.gly091020.GLYCan.client;

import com.gly091020.GLYCan.GLYCanMod;
import com.gly091020.GLYCan.block.CanBlock;
import com.gly091020.GLYCan.block.entity.CanBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;

import java.util.Random;

@OnlyIn(Dist.CLIENT)
public class CanBlockEntityRenderer implements BlockEntityRenderer<CanBlockEntity> {
    public static final ItemRenderer RENDERER = Minecraft.getInstance().getItemRenderer();
    @Override
    public void render(CanBlockEntity canBlockEntity, float v, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int i1) {
        Random random = new Random(canBlockEntity.getBlockPos().hashCode());
        var o = 0.05;
        if(canBlockEntity.isEmpty()){return;}
        for(ItemStack stack: canBlockEntity.getItems()){
            poseStack.pushPose();
            poseStack.translate(0.5, o, 0.5);
            poseStack.scale(0.45f, 1f, 0.45f);
            poseStack.mulPose(Axis.XP.rotationDegrees(90));
            poseStack.mulPose(Axis.ZP.rotationDegrees(random.nextFloat(0, 360)));
            RENDERER.renderStatic(stack, ItemDisplayContext.FIXED, i, i1, poseStack, multiBufferSource, canBlockEntity.getLevel(), canBlockEntity.getBlockPos().hashCode());
            poseStack.popPose();
            o += 0.07;
        }
        if(canBlockEntity.getBlockState().getValue(CanBlock.IS_TETO)){return;}
        var maxItem = canBlockEntity.getMaxItems();
        ItemStack f = null;
        ItemStack s = null;
        if (maxItem.size() == 1) {
            f = maxItem.getFirst();
        } else if(!maxItem.isEmpty()) {
            f = maxItem.getFirst();
            s = maxItem.get(1);
        }
        if (f != null) {
            renderLeftItem(canBlockEntity, v, poseStack, multiBufferSource, i, i1, f);
            renderRightItem(canBlockEntity, v, poseStack, multiBufferSource, i, i1, s == null ? f : s);
        }
    }

    public void renderLeftItem(CanBlockEntity canBlockEntity, float v, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int i1, ItemStack stack){
        var isNS = this.isNS(canBlockEntity);
        poseStack.pushPose();
        poseStack.translate(!isNS ? 0.74f : 0.5f, 0.4, isNS ? 0.74f : 0.5f);
        poseStack.scale(!isNS ? 0.1f : 0.45f, 0.45f, isNS ? 0.1f : 0.45f);
        poseStack.mulPose(Axis.YP.rotationDegrees(!isNS ? 90 : 0));
        RENDERER.renderStatic(stack, ItemDisplayContext.FIXED, i, i1, poseStack, multiBufferSource, canBlockEntity.getLevel(), canBlockEntity.getBlockPos().hashCode());
        poseStack.popPose();
    }

    public void renderRightItem(CanBlockEntity canBlockEntity, float v, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int i1, ItemStack stack){
        var isNS = this.isNS(canBlockEntity);
        poseStack.pushPose();
        poseStack.translate(!isNS ? 1-0.74 : 0.5f, 0.4, !isNS ? 0.5 : 1-0.74);
        poseStack.scale(!isNS ? 0.1f : 0.45f, 0.45f, isNS ? 0.1f : 0.45f);
        poseStack.mulPose(Axis.YP.rotationDegrees(!isNS ? 90 : 0));
        RENDERER.renderStatic(stack, ItemDisplayContext.FIXED, i, i1, poseStack, multiBufferSource, canBlockEntity.getLevel(), canBlockEntity.getBlockPos().hashCode());
        poseStack.popPose();
    }

    public boolean isNS(CanBlockEntity canBlockEntity){
        boolean isNS = false;
        if(canBlockEntity.getBlockState().is(GLYCanMod.CAN_BLOCK)){
            var f = canBlockEntity.getBlockState().getValue(CanBlock.FACING);
            if(f == Direction.NORTH || f == Direction.SOUTH){
                isNS = true;
            }
        }
        return isNS;
    }
}
